/*
 * Created by JFormDesigner on Sun Jun 30 09:17:17 HKT 2024
 */

package com.hongyan.view;

import java.awt.event.*;

import com.hongyan.base.BaseJpanel;
import com.hongyan.bean.*;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.dao.LogDao;
import com.hongyan.dao.LoginDao;
import com.hongyan.main.SwingMainFrame;
import sun.applet.Main;

import java.awt.*;
import java.sql.Date;
import java.sql.Timestamp;
import javax.swing.*;

/**
 * @author hongyan
 */
public class SwingLoginPanel extends BaseJpanel {

    private static String[] loginType = new String[]{"学生", "老师", "管理员"};

    public SwingLoginPanel(SwingMainFrame swingMainFrame) {
        super(swingMainFrame);
        initData();
    }

    private void login(ActionEvent e) {

    }


    @Override
    public void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        textField2 = new JPasswordField();
        label4 = new JLabel();
        comboBox1 = new JComboBox();
        button1 = new JButton();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("\u8f6f\u4ef6\u6d4b\u8bd5\u7ba1\u7406\u7cfb\u7edf");
        label1.setFont(new Font("\u6977\u4f53", Font.PLAIN, 18));
        add(label1);
        label1.setBounds(new Rectangle(new Point(135, 15), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d\uff1a");
        add(label2);
        label2.setBounds(new Rectangle(new Point(85, 65), label2.getPreferredSize()));
        add(textField1);
        textField1.setBounds(155, 60, 140, textField1.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u5bc6\u7801\uff1a");
        add(label3);
        label3.setBounds(85, 105, 52, 17);
        add(textField2);
        textField2.setBounds(155, 100, 140, textField2.getPreferredSize().height);

        //---- label4 ----
        label4.setText("\u7c7b\u578b\uff1a");
        add(label4);
        label4.setBounds(85, 145, 52, 17);
        add(comboBox1);
        comboBox1.setBounds(155, 140, 140, comboBox1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.addActionListener(e -> login(e));
        add(button1);
        button1.setBounds(150, 185, 103, 45);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    @Override
    public void initData() {
        comboBox1.setModel(new DefaultComboBoxModel(loginType));
        comboBox1.setSelectedIndex(0);
    }

    @Override
    public void initListen() {
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO add your code here
                Person login = LoginDao.login(textField1.getText(), textField2.getText(), comboBox1.getSelectedIndex());
                Log log = new Log();
                log.setL_id(MainFrameConfig.getSnowflakeUtils().getId());
                log.setL_name("登录");
                log.setRecord_time(new Timestamp(System.currentTimeMillis()));
                if (login == null) {
                    JOptionPane.showMessageDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
                    log.setContent(comboBox1.getSelectedItem() + ":" + textField1.getText() + "登录失败，账号或密码错误");
                } else {
                    getSwingMainFrame().replacePanel(new SwingContentPanel(getSwingMainFrame(), comboBox1.getSelectedIndex(), login instanceof Student ? ((Student) login).getS_id() : login instanceof Teacher ? ((Teacher) login).getT_id() : ((Admin) login).getA_id()));
                    log.setContent(comboBox1.getSelectedItem() + ":" + textField1.getText() + "登录成功");
                }
                LogDao.addLog(log);

            }
        });
//        button2.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                getSwingMainFrame().replacePanel(new SwingRegisterPanel(getSwingMainFrame()));
//            }
//        });
    }

    @Override
    public int getJpanelHeight() {
        return 300;
    }

    @Override
    public int getJpanelWidth() {
        return 400;
    }

    @Override
    public String title() {
        return "登录";
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JPasswordField textField2;
    private JLabel label4;
    private JComboBox comboBox1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
